import javax.microedition.lcdui.*;
// import javax.microedition.rms.RecordStore;  
// import javax.microedition.rms.RecordStoreException;
import java.util.Random;
import java.io.*;
import java.lang.Math;
// import javax.microedition.media.*;
// import javax.microedition.media.control.*;
#include "defines.h"
#include "Data.h"
#include "macro.h"
public class cGame extends Canvas implements Runnable, IDefines, IStates, IConstans
{
	public final long DELAY_TIME = 300;
	long m_lModueStartTime = 0;
	public static Graphics s_g;
	public VinaSoftSNK1 midlet;
	static int s_iWidth, s_iHeight;
	static Image s_imgBackground = null;
	static int s_iPressedKey = 0;
	static int s_iReleasedKey = 0;
	static int s_iRepeatedKey = 0;
	boolean m_isRunning = true;
	static Graphics s_gBackBuffer;
	static Image s_imgBackBuffer;

	
	static boolean softkeyLeft;
	static boolean softkeyRight;
	
	static int _languageCurrent = 0;	//EN
	static String[] LanguageList = {"EN","VN"};
	
	// int[] _menu_current = null;

// Include source files
	
	#include "cGame_StateMachine.h"
	#include "cGame_text.h"
	#include "State_Init.h"
	#include "input_manager.h"
	#include "cGame_Menus.h"
	#include "State_Logo.h"
	#include "State_SplashScreen.h"
	#include "State_MainMenu.h"
	#include "State_Quit.h"
	#include "State_SelectLanguage.h"
	#include "cGame_SelectLanguage.h"
	#include "State_GamePlay.h"
	#include "cGame_GamePlay.h"
	#include "State_IGM.h"
	#include "State_MM_Confirm.h"
	#include "cGame_ConfirmDialog.h"
	#include "State_Exit_Confirm.h"
	#include "cGame_update.h"
	#include "cGame_paint.h"

	public cGame()
	{	
		new Thread(this).start();
		setFullScreenMode(true);
		s_iWidth = getWidth();
		s_iHeight = getHeight();
		
		if(hasPointerEvents())
		{
			System.out.println(">>> can touch.");
		}
		else
		{
			System.out.println("<<< can't touch...");
		}
		
		StateMachine_Init();
		// s_iStateCurrent = STATE_LOGO;
		// s_iStatePrev = -2;
	}
	
	public static int MyRandNumber(int a, int b)
	{
		if(a>b)
			return -1;
		Random rand = new Random(System.currentTimeMillis());
		int n = rand.nextInt();
		if(n<0)
			n=-n;
		return a + n % (b-a);
	}
	
	protected void hideNotify()
	{
#ifdef USE_DEBUG
		System.out.println("-----> hideNotify()");
#endif
	}
	
	protected void showNotify()
	{
#ifdef USE_DEBUG
		System.out.println("-----> showNotify()");
#endif
	}
	
	public void run()
	{
		SwitchState(STATE_INIT);
		while(m_isRunning)
		{
			try{
				Thread.sleep(50);
				GameUpdate();
			}catch(Exception e){
			}
			repaint();
			serviceRepaints();
		}
		GameDestroy();
		// CreateStringCache("/TEXT_VN.LANG");
		
		midlet.Exit();
	}
	
	public void paint(Graphics g)
	{
		s_gBackBuffer = g;
		try{
			GamePaint();
		}catch(Exception ex){}
	}
	
	static public void GameDestroy()
	{
		// m_imgBackground = null;
		s_imgBackBuffer = null;
		s_g = null;
		s_gBackBuffer = null;
		s_sprMenu = null;
		System.gc();
		
		System.out.println("======= System gc =======");
	}
}